package cn.com.callback


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse

import cn.com.elite.EliteBean
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate
import cn.com.elite.Util

import javax.sql.DataSource

import org.springframework.web.context.support.WebApplicationContextUtils
import org.springframework.context.ApplicationContext

public class api_getvoice_content2 implements ServiceBackInvoke{

	private static final Log log = LogFactory.getLog(api_getvoice_content2.class);

	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {
			ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
			JdbcTemplate jt = new JdbcTemplate((DataSource)ctx.getBean("dataSource"));   //获取数据源
	
			response.setCharacterEncoding("UTF-8");
			log.debug("-------------------------开始获取停水通知/语音播报内容-----------------");
			String getMethod = request.getMethod();
			String callJson="";
			BufferedReader iny = null;
			String dealMessage = "";
			String dealResult = "";
	
			if (getMethod.equals("GET")) {
				callJson = request.getParameter("jsondata");
				log.debug("原始Get请求参数:"+callJson);
			} else {
				InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
				iny = new BufferedReader(reader);
				String line;
				while ((line = iny.readLine()) != null) {
					callJson += line;
				}
				log.debug("原始Post请求参数:"+callJson);
			}
	
			try{
				JSONObject obj = JSONObject.fromObject(callJson);
				def areatype = Util.filterForObject(obj.get("areatype"));
				log.debug("areatype:"+areatype);
				def typeid = Util.filterForObject(obj.get("typeid"));
				log.debug("typeid:"+typeid);
				def type = Util.filterForObject(obj.get("type"));
				log.debug("type:"+type);
				if(isNull(type)) {
					dealResult = "0";
					dealMessage = isNullMSG("type", type);
					return(dealMessage);
					
				}
				else {
				
				if(type=='voice') {
					if(isNull(typeid)) {
						dealResult = "0";
						dealMessage =isNullMSG("typeid", typeid);
						return(dealMessage);
					}else {
							try {
					//查询语音播报内容
								
			   String GetContentListSQL = "SELECT template_desc FROM sysc_template1 where template_type in (select type_id from sysc_templatetype where 1=1 and type_id in ('SB0001','SB0002','SB0003','SB0004') and  CHARINDEX(rtrim(area_type),'475AE8F5-8C60-0947-96D6-D4999CE07DF4,95ED5385-7379-1041-919A-BDF10A1CB5DC,ED48B3EA-347F-684F-99A3-9C0BA66B744F,74BCB8CD-D886-7B4D-BE0E-AAECE71FA6DE')>0) and template_type='" + typeid + "'";
			List GetContentList = eliteBean.chaxun_sql(GetContentListSQL,"dataSource");
			JSONObject GetContentJson = new JSONObject();
			if(GetContentList.size() > 0) {
				for(int i = 0; i < GetContentList.size(); i++) {
					
					
					GetContentJson.put("content", GetContentList.get(i).get("template_desc"));
					  }
					  String GetContentJsonStr=GetContentJson.toString();
					  log.debug( "调用发送语音播报内容报文信息：" + GetContentJsonStr);
					  return "{\"code\": 1,\"message\": \"成功\",\"data\":'" + GetContentJsonStr + "'}";
					}
					else {
						return "{\"code\": 1,\"message\": \"无匹配条件的语音播报内容，请检查参数！\"}";
						}
							}
							catch(Exception e) {
								log.error("接口处理异常"+e.getMessage());
								return "{\"code\": 0,\"message\": \"查询异常\",\"data\": null}";
								
							}
					}
					
				}
				else if(type=='notice') {
					if(isNull(areatype)||isNull(typeid)) {
						dealResult = "0";
						dealMessage = isNullMSG("areatype", areatype) + isNullMSG("typeid", typeid);
						return(dealMessage);
					}else {
							try {
					
			//调用存储过程处理IVR 停水通知 播报内容
			log.debug("SMS_Link调用存储过程处理停水通知数据...");
			def procedure="{call Dbo.SMS_Link(?,?)}";
			def inputMap = new String[1];
			inputMap[0] = "SMS_Link";

			log.debug("SMS_Link调用存储过程处理SQL:" + procedure);
			def reSult = eliteBean.executeWithReturn(procedure, inputMap, 'dataSource');
			log.debug("SMS_Link调用存储过程处理返回:" + reSult);
			
			//查询停水通知内容
			String GetContentListSQL = "select template_desc from sysc_template s left join sysc_templatetype t on s.template_type = t.type_id where 1=1 and template_type='" + typeid + "' and area_type='" + areatype + "'";
			List GetContentList = eliteBean.chaxun_sql(GetContentListSQL,"dataSource");
			JSONObject GetContentJson = new JSONObject();
			if(GetContentList.size() > 0) {
				for(int i = 0; i < GetContentList.size(); i++) {
					
					
					GetContentJson.put("content", GetContentList.get(i).get("template_desc"));
					  }
					  String GetContentJsonStr=GetContentJson.toString();
					  log.debug( "调用发送停水通知内容报文信息：" + GetContentJsonStr);
					  return "{\"code\": 1,\"message\": \"成功\",\"data\":'" + GetContentJsonStr + "'}";
					}
					else {
						return "{\"code\": 1,\"message\": \"无匹配条件的停水通知内容，请检查参数！\"}";
						}
							}
							catch(Exception e) {
								log.error("接口处理异常"+e.getMessage());
								return "{\"code\": 0,\"message\": \"查询异常\",\"data\": null}";
								
							}
					}
					
				}
				
				

			}
			
			
			}
	catch(Exception e)
	{
		log.error("接口处理异常"+e.getMessage());
		return "{\"code\": 0,\"message\": \"查询异常\",\"data\": null}";
	  }
	}
	// 非空校验isNull
	public static boolean isNull(String value) {

		boolean reFlag = true;
		if (value == "" || value.length() <= 0) {
			reFlag = true;
		} else {
			reFlag = false;
		}
		log.debug(reFlag);
		return reFlag;
	}

	// 非空校验isNullMSG
	public static String isNullMSG(String key, String value) {

		String reResult = "";
		if (value == "" || value.length() <= 0) {
			reResult = key + "必填字段;";
		}
		return reResult;
	}

}
